import ala from 'ala'
export default {
	// 转换列
	toColumns(columns, data) {
		if (!columns) {
			return
		}
		columns = columns.filter(r => r.type !== 'hidden')
		var keyValueField
		for (var i = 0; i < columns.length; i++) {
			var element = columns[i]
			if (element.columnStyle === 12 && data[0]) {
				var keyValues = data[0][element.field]
				if (keyValues) {
					keyValueField = element.field
					columns.splice(i, 1)
					keyValues.forEach((k, keyIndex) => {
						var addItem = {
							...element
						}
						addItem.field = k.name
						addItem.columnStyle = 0
						addItem.name = k.name
						columns.splice(i + keyIndex, 0, addItem)
					})
				}
				break
			}
		}
		var result = {
			columns,
			keyValueField
		}
		return result
	},
	// 数据转换keyvalue对象
	toData(data, keyValueField) {
		if (keyValueField) {
			data.forEach(r => {
				var keyValues = r[keyValueField]
				if (keyValues) {
					keyValues.forEach(k => {
						r[k.name] = k.value
					})
				}
			})
		}
		return data
	}
}